<?php
require_once '../classes/includeAll.php';
require '../resources/getConnection.php';

$configClass = new Config($connection);

if(isset($_POST['submit']))
{
   if(isset($_POST['switch']))
   {
       $switchArray = $_POST['switch'];
       foreach ($switchArray as $SwtichChange) 
       {
           if(isset($SwtichChange[0])  )
           {  
               $configSwitch = new ConfigSwitch($SwtichChange[0], $connection);
               if(isset($SwtichChange[1]))
               {
                if($SwtichChange[1] != "")
                {
                  $configSwitch->UpdateSwitch($configSwitch->IP, $SwtichChange[1]);
                  $configClass->EntryExistsOrCreate ("OutOfSwitchSync", 1);
                }
               }

               if(isset($SwtichChange[2]))
               {
                if($SwtichChange[2] != "")
                {
                  $configSwitch->UpdateSwitch($SwtichChange[2], $configSwitch->Name); 
                  $configClass->EntryExistsOrCreate ("OutOfSwitchSync", 1);
                }   
               }
           }
       }
   }
   if(isset($_POST['switchNew']))
   {
       $newSwitch = $_POST['switchNew'];
       foreach ($newSwitch as $switch) 
       {
           if(isset($switch[0]) && isset($switch[1]))
           {
               if($switch[0] != "" && $switch[1] != "")
               {
                   $configSwitch = new ConfigSwitch(0, $connection);
                   $configSwitch->newSwitch($switch[1], $switch[0]);
                   $configClass->EntryExistsOrCreate ("OutOfSwitchSync", 1);
               }
           }
           
       }
   }
}
    
if(isset($_POST['addVlan']))
{
   if(isset($_POST['vlan']))
   {
       $newVlans = $_POST['vlan'];
       foreach ($newVlans as $vlan) 
       {
           if(isset($vlan[0]) && isset($vlan[1]))
           {
               if(is_numeric($vlan[0]) && $vlan[1] != "")
               {
                   $switchesClass = new configSwitches($connection);
                   $switchesClass->AddVlan($vlan[0], $vlan[1]);
               }
           }           
       }
   }
}

 $connection->disconnectDB();


/////Header & Menu
$title = "Switch Konfiguration";
$head= "
<link rel=\"stylesheet\" type=\"text/css\" href=\"../design/css/configSwitch.css\">
<script type=\"text/javascript\" src=\"../javascript/pagesJavascript/configSwitch.js\"></script>
        ";
require_once '../menu/head.php';
require '../resources/getConnection.php';

$switchesClass = new configSwitches($connection);
$switches = $switchesClass->GetAllSwitches();

//Content

if(isset($_GET['VlanConfig']))
{
   if($_GET['VlanConfig'] == "true")
   {
echo"
<a href=\"configSwitch.php\">zur&uuml;ck</a>
<h1>Switch Konfiguration Vlan Liste</h1> 
";
$vlans = $switchesClass->GetAllVlans();
echo
"
<form action=\"configSwitch.php?VlanConfig=true\" method=\"POST\">
    <table class=\"vlanList\">
    <thead>
        <tr>
            <th>
                Vlan Nummer
            </th>
            <th>
                Vlan Name
            </th>
            <th>
            </th>
        </tr>
    </thead>
    <tbody> 
";
foreach ($vlans as $vlan) 
{
echo 
"
    <tr>
        <td>
            ".sanitizeOutputString($vlan[2])."
        </td>
        <td>
            ".sanitizeOutputString($vlan[1])."
        </td>
        <td onclick=\"deleteVlanFromList(this, ". $vlan[0] .")\" > <img width=\"20px\" src=\"../design/pictures/delete.png\"> </td>
    </tr>
";
    
}
echo 
"       <tr>
            <td><input type=\"text\" value=\"\" onchange=\"changeVlanInputField(this)\" name=\"vlan[0][0]\" id=\"vlanNumber0\"  size=\"20px\" /></td>
            <td><input type=\"text\" value=\"\" onchange=\"changeVlanInputField(this)\" name=\"vlan[0][1]\" id=\"vlanName0\"  size=\"20px\"/></td>
            <td></td>
        </tr>
        <tr>
            <td></td>
            <td><input type=\"submit\" value=\"Hinzuf&uuml;gen\" name=\"addVlan\" /></td>       
            <td></td>
        </tr>
</tbody>
</table>
</form>";      
   }
} else {
echo"
<h1>Switch Konfiguration</h1>
 <form action=\"configSwitch.php\" method=\"POST\">
";
$scriptIndex=0;
$index=0;
foreach ($switches as $cSwitch) 
{
    
    //ChangeFlag
    $flagMeaning = (new allFlags())->SwitchFlags;
    $state="";
    if($cSwitch->ChangeFlag == 0x02)
    {
        $state = '<img src="../design/pictures/error.png" width="16" alt="X"/>&nbsp;';
        $state .= array_search($cSwitch->ChangeFlag, $flagMeaning);
        $state .= ' <img src="../design/pictures/delete.png" width="16" alt="X"/>';
    }

    //END
echo"
    <table class=\"switch\">
        <thead>
            <tr>
                <th></th>
                <th>Switch Name:</th>
                <th>IP Adresse:</th>
                <th>Meldung:</th>
                <th>Vlans:</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td onclick=\"deleteSwitch(this, ". $cSwitch->ID .")\" > <img width=\"20px\" src=\"../design/pictures/delete.png\"> </td>
                <td onclick='insertTextField(this,\"switch[$index][1]\",$scriptIndex)'>". sanitizeOutputString($cSwitch->Name) ."</td>";$scriptIndex++; echo "
                <td onclick='insertTextField(this,\"switch[$index][2]\",$scriptIndex)'>". sanitizeOutputString($cSwitch->IP) ."</td>
                <td"; if($cSwitch->ChangeFlag == 0x02){echo' onclick="chagneState(this, '. $cSwitch->ID .')" ';}echo"> $state  </td>";
                
                $scriptIndex++; echo "
                <input type=\"hidden\" name=\"switch[$index][0]\" value=\"$cSwitch->ID\">
                <td>          
                    <table class=\"vlan\">
";
if(count($cSwitch->Vlan)>= 1)
{
    foreach ($cSwitch->Vlan as $vlan) 
    {echo"
                        <tr>
                            <td>". sanitizeOutputString($vlan[1]) ."</td>
                            <td>". sanitizeOutputString($vlan[2]) ."</td>
                            <td onclick=\"deleteVlan(this, ". $vlan[0] .",".$cSwitch->ID.")\" > <img width=\"20px\" src=\"../design/pictures/delete.png\"></td>
                        </tr>
"; }} echo"             <tr>
                            <td>
                                <select id=\"selectVlan$index\">
";                  
$vlans=$switchesClass->GetAllVlans();
foreach ($vlans as $allVlan) 
{
    echo"<option value=\"". $allVlan[0]."\">". sanitizeOutputString($allVlan[1])." " .sanitizeOutputString($allVlan[2]) . "</option>";  
}
echo"
                                 </select>
                            </td>
                            <td><input type=\"button\" value=\"Hinzuf&uuml;gen\" name=\"vlanAdd\" onclick=\"addVlan(this, $index,".$cSwitch->ID.")\" /></td>
                            <td></td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        </tbod>
    </table><br>
    "; 
    $index++;
}
echo"
<table class=\"switch\">
    <thead>
        <tr>
            <th></th>
            <th>Switch Name:</th>
            <th>IP Adresse:</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td></td>
            <td><input type=\"text\" value=\"\" onchange=\"changeSwitchInputField(this)\" name=\"switchNew[0][0]\" id=\"switchName0\"  size=\"20px\" /></td>
            <td><input type=\"text\" value=\"\" onchange=\"changeSwitchInputField(this)\" name=\"switchNew[0][1]\" id=\"switchIP0\"  size=\"20px\" /></td>
            <td>
            </td>
        </tr>
        <tr>
            <td></td>
            <td><input type=\"submit\" value=\"Übernehmen & Hinzuf&uuml;gen\" name=\"submit\" /></td>
            <td></td>
            <td><input type=\"button\" value=\"Vlan Tabelle\" name=\"vlanTable\" onclick=\"location.href = 'configSwitch.php?VlanConfig=true'\" /></td>
        </tr>
    </tbody>
</table>
</form>";}






//Disconect DB & Footer
 $connection->disconnectDB();
require_once '../menu/footer.php';
?>


